﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Algoritmia.Problems.ActivitySelection
{
    public class ActivitiesSelector : IActivitiesSelector
    {
        #region Implementation of IActivitiesSelector

        public IEnumerable<Tuple<double, double>> Select(IEnumerable<Tuple<double, double>> activities)
        {
            var activitiesList = activities.ToList();
            var t = activitiesList.Min(tp => tp.Item1);
            activitiesList.Sort( (tp1,tp2) => tp1.Item2.CompareTo(tp2.Item2));
            foreach(var activity in activitiesList)
            {
                if ( t <= activity.Item1)
                {
                    yield return activity;
                    t = activity.Item2;
                }
            }
        }

        #endregion
    }
}
